﻿package PinballCode {
	
	import flash.display.Shape;
	
	// Polygon creates n sides polygons 
	public class Polygon extends Shape {
		private var radius:Number;     // radius from center of polygon
		private var center:Vertex;     // center of polygon
		private var sides:Number;      // number of sides
		private var color:uint;        // color of polygon
		private var thickness:Number;  // thickness of outer line
		private var vertices:Array;    // array of polygon vertices
		private var hollow:Boolean;    // is the polygon hollow?
		
		// Constructor
		public function Polygon(sides:uint, center:Vertex, radius:Number=40, thickness:Number=1, color:uint=0xff0000, hollow:Boolean=false) {
			this.sides = sides;
			this.center = center;
			this.radius = radius;
			this.color = color;
			this.thickness = thickness;
			this.hollow = hollow;
			vertices = new Array(); 
			init();
		}
		
		
		// initalizes the game
		public function init():void {
			makeVertices();
			renderVertices();
		}
		
		// calculate and store polygon vertices:
		public function makeVertices():void {
			vertices.push(new Vertex(center.x+radius,center.y)); // start polygon on x=radius and y=0 from center
			
			// calculate vertices for angles between 0 and 360, not inclusive--already have 1st vertex, which closes polygon)
			for (var side:uint=1; side < sides; side++) {
				var angle:Number = 2*Math.PI*side / sides; // how many angle increments from 0
				vertices.push(new Vertex(center.x+radius*Math.cos(angle),center.y+radius*Math.sin(angle)));
			}
			
		}
		
		// render the polygon:
		public function renderVertices():void {
			
			// set fill and hollowness:
			graphics.beginFill(color,alpha=(hollow?0:1));
			
			// set border:
			graphics.lineStyle(thickness,color,alpha=1);
			
			// start at this vertex (x=radius,y=0 from center)
			graphics.moveTo(Vertex(vertices[0]).x, Vertex(vertices[0]).y);
			
			// draw line to each vertex going clockwise from starting vertex:
			for(var side:uint=1; side < sides; side++) {
				graphics.lineTo(Vertex(vertices[side]).x, Vertex(vertices[side]).y);
			}
			
			// connect the last vertex to starting vertex:
			graphics.lineTo(Vertex(vertices[0]).x, Vertex(vertices[0]).y);
			
			// done:
			graphics.endFill();
		}
		
		
		
		
	}
}